
/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2022原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.aop.framework;

import org.aopalliance.intercept.Interceptor;

import org.springframework.aop.TargetSource;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
 * Factory for AOP proxies for programmatic use, rather than via declarative
 * setup in a bean factory. This class provides a simple way of obtaining
 * and configuring AOP proxy instances in custom user code.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 14.03.2003
 */
/**
 *面向AOP代理的工厂，用于编程使用，而不是通过声明性
 *在bean工厂中设置。此类提供了获取
 *以及在自定义用户代码中配置AOP代理实例。
 *
 *@作者Rod Johnson
 *@作者Juergen Hoeller
 *@作者Rob Harrop
 *@自2003年3月14日
 */
@SuppressWarnings("serial")
public class ProxyFactory extends ProxyCreatorSupport {

	/**
	 * Create a new ProxyFactory.
	 */
	/**
	 *创建新的ProxyFactory。
	 */
	public ProxyFactory() {
	}

	/**
	 * Create a new ProxyFactory.
	 * <p>Will proxy all interfaces that the given target implements.
	 * @param target the target object to be proxied
	 */
	/**
	 *创建新的ProxyFactory。
	 *＜p＞将代理给定目标实现的所有接口。
	 *@param target要代理的目标对象
	 */
	public ProxyFactory(Object target) {
		setTarget(target);
		setInterfaces(ClassUtils.getAllInterfaces(target));
	}

	/**
	 * Create a new ProxyFactory.
	 * <p>No target, only interfaces. Must add interceptors.
	 * @param proxyInterfaces the interfaces that the proxy should implement
	 */
	/**
	 *创建新的ProxyFactory。
	 *＜p＞没有目标，只有接口。必须添加拦截器。
	 *@param proxyInterface代理应实现的接口
	 */
	public ProxyFactory(Class<?>... proxyInterfaces) {
		setInterfaces(proxyInterfaces);
	}

	/**
	 * Create a new ProxyFactory for the given interface and interceptor.
	 * <p>Convenience method for creating a proxy for a single interceptor,
	 * assuming that the interceptor handles all calls itself rather than
	 * delegating to a target, like in the case of remoting proxies.
	 * @param proxyInterface the interface that the proxy should implement
	 * @param interceptor the interceptor that the proxy should invoke
	 */
	/**
	 *为给定的接口和拦截器创建一个新的ProxyFactory。
	 *＜p＞为单个拦截器创建代理的方便方法，
	 *假设拦截器自己处理所有调用，而不是
	 *委托给目标，就像远程代理的情况一样。
	 *@param proxyInterface代理应实现的接口
	 *@param interceptor代理应调用的拦截器
	 */
	public ProxyFactory(Class<?> proxyInterface, Interceptor interceptor) {
		addInterface(proxyInterface);
		addAdvice(interceptor);
	}

	/**
	 * Create a ProxyFactory for the specified {@code TargetSource},
	 * making the proxy implement the specified interface.
	 * @param proxyInterface the interface that the proxy should implement
	 * @param targetSource the TargetSource that the proxy should invoke
	 */
	/**
	 *为指定的｛@code TargetSource｝创建ProxyFactory，
	 *使代理实现指定的接口。
	 *@param proxyInterface代理应实现的接口
	 *@param targetSource代理应调用的targetSource
	 */
	public ProxyFactory(Class<?> proxyInterface, TargetSource targetSource) {
		addInterface(proxyInterface);
		setTargetSource(targetSource);
	}


	/**
	 * Create a new proxy according to the settings in this factory.
	 * <p>Can be called repeatedly. Effect will vary if we've added
	 * or removed interfaces. Can add and remove interceptors.
	 * <p>Uses a default class loader: Usually, the thread context class loader
	 * (if necessary for proxy creation).
	 * @return the proxy object
	 */
	/**
	 *根据此工厂中的设置创建新代理。
	 *＜p＞可重复调用。如果我们添加了
	 *或移除的接口。可以添加和删除拦截器。
	 *＜p＞使用默认的类加载器：通常，线程上下文类加载器
	 *（如果需要创建代理）。
	 *@返回代理对象
	 */
	public Object getProxy() {
		return createAopProxy().getProxy();
	}

	/**
	 * Create a new proxy according to the settings in this factory.
	 * <p>Can be called repeatedly. Effect will vary if we've added
	 * or removed interfaces. Can add and remove interceptors.
	 * <p>Uses the given class loader (if necessary for proxy creation).
	 * @param classLoader the class loader to create the proxy with
	 * (or {@code null} for the low-level proxy facility's default)
	 * @return the proxy object
	 */
	/**
	 *根据此工厂中的设置创建新代理。
	 *＜p＞可重复调用。如果我们添加了
	 *或移除的接口。可以添加和删除拦截器。
	 *＜p＞使用给定的类加载器（如果需要创建代理）。
	 *@param classLoader创建代理的类加载器
	 *（或｛@code null｝表示低级代理工具的默认值）
	 *@返回代理对象
	 */
	public Object getProxy(@Nullable ClassLoader classLoader) {
		return createAopProxy().getProxy(classLoader);
	}

	/**
	 * Determine the proxy class according to the settings in this factory.
	 * @param classLoader the class loader to create the proxy class with
	 * (or {@code null} for the low-level proxy facility's default)
	 * @return the proxy class
	 * @since 6.0
	 */
	/**
	 *根据此工厂中的设置确定代理类。
	 *@param classLoader创建代理类的类加载器
	 *（或｛@code null｝表示低级代理工具的默认值）
	 *@return代理类
	 *@自6.0起
	 */
	public Class<?> getProxyClass(@Nullable ClassLoader classLoader) {
		return createAopProxy().getProxyClass(classLoader);
	}


	/**
	 * Create a new proxy for the given interface and interceptor.
	 * <p>Convenience method for creating a proxy for a single interceptor,
	 * assuming that the interceptor handles all calls itself rather than
	 * delegating to a target, like in the case of remoting proxies.
	 * @param proxyInterface the interface that the proxy should implement
	 * @param interceptor the interceptor that the proxy should invoke
	 * @return the proxy object
	 * @see #ProxyFactory(Class, org.aopalliance.intercept.Interceptor)
	 */
	/**
	 *为给定接口和拦截器创建新代理。
	 *＜p＞为单个拦截器创建代理的方便方法，
	 *假设拦截器自己处理所有调用，而不是
	 *委托给目标，就像远程代理的情况一样。
	 *@param proxyInterface代理应实现的接口
	 *@param interceptor代理应调用的拦截器
	 *@返回代理对象
	 *@see#ProxyFactory（类，org.aopalliance.incept.Interceptor）
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor) {
		return (T) new ProxyFactory(proxyInterface, interceptor).getProxy();
	}

	/**
	 * Create a proxy for the specified {@code TargetSource},
	 * implementing the specified interface.
	 * @param proxyInterface the interface that the proxy should implement
	 * @param targetSource the TargetSource that the proxy should invoke
	 * @return the proxy object
	 * @see #ProxyFactory(Class, org.springframework.aop.TargetSource)
	 */
	/**
	 *为指定的｛@code TargetSource｝创建代理，
	 *实现指定的接口。
	 *@param proxyInterface代理应实现的接口
	 *@param targetSource代理应调用的targetSource
	 *@返回代理对象
	 *@see#ProxyFactory（类，org.springframework.aop.TargetSource）
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource) {
		return (T) new ProxyFactory(proxyInterface, targetSource).getProxy();
	}

	/**
	 * Create a proxy for the specified {@code TargetSource} that extends
	 * the target class of the {@code TargetSource}.
	 * @param targetSource the TargetSource that the proxy should invoke
	 * @return the proxy object
	 */
	/**
	 *为扩展的指定｛@code TargetSource｝创建代理
	 *｛@code TargetSource｝的目标类。
	 *@param targetSource代理应调用的targetSource
	 *@返回代理对象
	 */
	public static Object getProxy(TargetSource targetSource) {
		if (targetSource.getTargetClass() == null) {
			throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
		}
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTargetSource(targetSource);
		proxyFactory.setProxyTargetClass(true);
		return proxyFactory.getProxy();
	}

}
